<template>
  <div class="app-container">
    <div class="form-container">
      <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input
            v-model="queryParams.roleName"
            size="small"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input
            v-model="queryParams.userName"
            size="small"
            placeholder
            clearable
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="btns-group">
      <el-button v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery">搜索</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery">重置</el-button>
      <el-button icon="el-icon-plus" size="middle" @click="handleAdd">新增</el-button>
      <el-button
        icon="el-icon-delete"
        size="middle"
        :disabled="multiple"
        @click="handleDelete"
      >取消授权用户</el-button>
      <span class="personal-type">
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>
      </span>
    </div>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      class="table-list"
      stripe
      :class="needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll'"
      :data="roleUserList"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column
        prop="loginName"
        label="登录账号"
        align="center"
      />
      <el-table-column
        prop="userName"
        label="用户名称"
        align="center"
      />
      <el-table-column prop="dept.deptName" label="部门" align="center" />
      <el-table-column
        prop="phonenumber"
        label="手机号码"
        align="center"
      />
      <el-table-column prop="status" label="状态" align="center" :formatter="statusFormat" />
      <el-table-column prop="createTime" label="创建时间" align="center" />
    </el-table>

    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 角色分配用户添加功能-->
    <role-add-user v-if="addUserVisible" ref="addUser" />
  </div>

</template>

<script>
import { listRoleUser, delRoleUser } from '@/api/system/role/role-user'
// 角色添加用户
import RoleAddUser from '@/views/system/role/components/RoleAddUser'
import { mapState } from 'vuex'
export default {
  name: 'RoleAddUser',
  components: { 'role-add-user': RoleAddUser },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的行对象
      multipleSelection: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 角色分配用户表格数据
      roleUserList: [],
      // 全部角色
      roleOptions: [],
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.fristPageSize,
        roleId: '',
        roleName: '',
        userName: '',
        status: ''
      },
      addUserVisible: false
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  mounted: function() {
    this.getDicts('sys_normal_disable').then(response => {
      this.statusOptions = [{ dictValue: '', dictLabel: '全部' }, ...response.data.rows]
    })
    // 路由带过来的参数
    const roleId = this.$route.query.roleId
    const roleName = this.$route.query.roleName
    // 搜索框中的roleId
    this.queryParams.roleId = roleId
    this.queryParams.roleName = roleName
    this.getList()
  },
  methods: {
    // 查询用户兼职部门list
    getList() {
      this.loading = true
      listRoleUser(this.queryParams).then(response => {
        this.roleUserList = response.data.rows
        this.total = response.data.total
        this.loading = false
        // this.addTaleData()
      })
    },
    addTaleData() {
      const arr = {}
      this.roleUserList.push(arr)
      if (this.roleUserList.length < this.queryParams.pageSize) {
        this.addTaleData()
      }
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置
    resetQuery() {
      this.queryParams.userName = ''
      this.queryParams.status = ''
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (JSON.stringify(selection[0]) === '{}') {
        selection = []
        this.$refs.multipleTable.clearSelection()
      }
      this.multipleSelection = selection
      this.ids = selection.map(item => item.userId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      console.log(this.multiple, this.single)
      if (selection.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(selection.pop())
      }
    },
    /** 角色添加用户 */
    handleAdd() {
      this.addUserVisible = true
      const roleId = this.queryParams.roleId
      this.$nextTick(() => {
        this.$refs.addUser.showUser(roleId)
      })
    },
    handleDelete() {
      this.loading = true
      const child = this.multipleSelection
      let ids = ''
      for (let i = 0; i < child.length; i++) {
        ids += child[i].userId + ','
      }
      // 去掉最后一个逗号
      if (ids.length > 0) {
        ids = ids.substr(0, ids.length - 1)
      }
      delRoleUser(this.queryParams.roleId, ids).then(resp => {
        this.loading = false
        if (resp) {
          this.msgSuccess('删除成功！')
          // 重新加载表格数据
          this.getList()
        } else {
          this.msgError(resp.message)
        }
      })
    },
    // 返回
    handleReturn() {
      this.$router.push({
        path: '/hr/role',
        name: '角色管理'
      })
    }

  }
}
</script>
